<template>
  <!--  试验计划-新增  -->
  <el-dialog title="新增" :visible.sync="dialogVisible" width="30%">
    <el-form ref="ruleForm" :model="form" :rules="rule" label-width="100px">
      <el-form-item label="站点" prop="site_id">
        <el-select v-model="form.site_id" size="mini" placeholder="请选择" style="width: 100%">
          <el-option
            v-for="item in site_options"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="计划标题" prop="plan_title">
        <el-input type="text" size="mini" v-model="form.plan_title" placeholder="请输入计划标题"></el-input>
      </el-form-item>
      <el-form-item label="计划内容" prop="plan_content">
        <el-input type="textarea" size="mini" v-model="form.plan_content" placeholder="请输入计划内容"></el-input>
      </el-form-item>
      <el-form-item label="计划周期" prop="plan_cycle">
        <el-select v-model="form.plan_cycle" size="mini" placeholder="请选择" style="width: 100%">
          <el-option
            v-for="item in cycle_options"
            :key="item.cycle_value"
            :label="item.cycle_label"
            :value="item.cycle_value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="demonstration" prop="plan_start" label="开始时间">
        <el-date-picker
          v-model="form.plan_start"
          type="date"
          size="mini"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
          style="width: 100%">
        </el-date-picker>
      </el-form-item>
      <el-form-item class="demonstration" prop="plan_end" label="结束时间">
        <el-date-picker
          size="mini"
          v-model="form.plan_end"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
          style="width: 100%">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" size="mini" @click="resetForm">取消</el-button>
      <el-button type="primary" size="mini" @click="addOrEdit">确定</el-button>
    </span>
  </el-dialog>

</template>

<script>
import { allSite, createPlan,updatePlan } from '@/api/operations'

export default {
  name: 'operationsExplanAdded',
  props:['titles','details'],
  data() {
    return {
      dialogVisible: false,
      form:{
        plan_type: 2,
        site_id:'',
        plan_title:'',
        plan_content:'',
        plan_cycle:'',
        plan_start:'',
        plan_end:''
      },
      site_options: [], // 站点数据
      cycle_options: [{
        cycle_value: 1,
        cycle_label: '单次'
      }, {
        cycle_value: 2,
        cycle_label: '每周'
      }, {
        cycle_value: 3,
        cycle_label: '每月'
      }],
      rule: {
        site_id: [
          {required: true, message: '请选择站点', trigger: 'blur'}
        ],
        plan_title: [
          {required: true, message: '请输入计划名称', trigger: 'blur'}
        ],
        plan_content: [
          {required: true, message: '请输入计划内容', trigger: 'blur'}
        ],
        plan_cycle: [
          {required: true, message: '请选择计划周期', trigger: 'blur'}
        ],
        plan_start: [
          {required: true, message: '请选择计划开始时间', trigger: 'blur'}
        ],
        plan_end: [
          {required: true, message: '请选择计划结束时间', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    this.getAllSite()
  },
   watch:{
    details(oldV,newV){
      console.log('接收的title',this.details)
      if(this.details.id){
        this.form=this.details
      }else{
        this.form={
        plan_type: 2,
        site_id:'',
        plan_title:'',
        plan_content:'',
        plan_cycle:'',
        plan_start:'',
        plan_end:''
      }
      }
      console.log('接收的titl11',this.form)
    }
  },
  methods:{
    //获取所有站点
    getAllSite() {
      allSite().then(res => {
        this.site_options = res.data['data']
      })
    },
    // 新增或编辑
    addOrEdit() {
      // this.$refs['ruleForm'].validate((valid) => {
      //   if (valid) {
      //     createPlan(this.form).then(res => {
      //       this.$message({
      //         message: '新增成功',
      //         type: 'success'
      //       })
      //       this.$emit('succ')
      //       this.resetForm()
      //     })
      //   }
      // })
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          if(this.titles=='新增'){
             createPlan(this.form).then(res => {
            this.$message({
              message: '新增成功',
              type: 'success'
            })
            this.$emit('getList')
            this.resetForm()
          })
          }else{
              updatePlan(this.form).then(res => {
            this.$message({
              message: '编辑成功',
              type: 'success'
            })
            this.$emit('getList')
            this.resetForm()
          })
          }
         

        }
      })
    },
    // 打开弹窗
    open(row) {
      // 修改打开弹窗的变量为true，就可以打开弹窗了
      this.dialogVisible = true;
    },
    // 关闭弹窗的方法
    close() {
      // 关闭弹窗的变量为false，就可以关闭弹窗了
      this.dialogVisible = false;
      // this.$emit('close')
    },
    // 表单重置
    resetForm() {
      this.$refs['ruleForm'].resetFields()
      this.dialogVisible = false;
    }
  }
}
</script>

<style scoped>

</style>
